/*
 * @Project Name: shoplook
 * @File Name: MaterialController
 * @Package Name: com.sm.shoplook.controller.system
 * @Date: 2018/8/13 14:06
 * @Creator: linshixing-1199
 * @line------------------------------
 * @修改人:
 * @修改时间:
 * @修改内容:
 */

package com.sm.shoplook.controller.base;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.domain.base.Material;
import com.sm.shoplook.service.base.MaterialCategoryService;
import com.sm.shoplook.service.base.MaterialService;
import com.sm.shoplook.util.FileUtil;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.base.material.req.MaterialSearchReqVo;
import com.sm.shoplook.vo.base.material.resp.MaterialRespVo;

/**
 * @author chenpy
 * @desc 素材管理
 * @date 2018/8/13 14:06
 * @see
 */
@Controller
@RequestMapping("/material")
public class MaterialController {

	private static final Logger LOGGER = LoggerFactory.getLogger(MaterialController.class);
	@Autowired
	private MaterialService materialService;
	@Autowired
	private MaterialCategoryService materialCategoryService;
	@Autowired
	private FileUtil fileUtil;

	@RequestMapping(value = "/manager", method = RequestMethod.GET)
	public String materialManger() {
		return "/base/material_manager";
	}

	/**
	 * @param materialSearchReqVo
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 素材列表
	 */
	@RequestMapping(value = "/pageList", method = RequestMethod.POST)
	@ResponseBody
	public ApiResult<Map<String, Object>> pageList(MaterialSearchReqVo materialSearchReqVo) {
		Map<String, Object> map = new HashMap<>();
		ApiResult<PageInfo<MaterialRespVo>> pageInfoApiResult = materialService
				.selectMaterialsPage(materialSearchReqVo);
		PageInfo<MaterialRespVo> pageInfo = pageInfoApiResult.getData();
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, map);
	}

	/**
	 * @param materialId
	 * @param model
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 跳转素材保存页面
	 */
	@RequestMapping(value = "/save_material_page/{materialId}", method = RequestMethod.GET)
	public String addOrUpdateRolePage(@PathVariable(value = "materialId") Integer materialId, Model model) {
		LOGGER.info("materialId: {}", materialId);
		ApiResult<MaterialRespVo> materialApiResult = materialService.selectMaterialById(materialId);
		MaterialRespVo materialRespVo = materialApiResult.getData();
		if (materialRespVo == null) {
			materialRespVo = new MaterialRespVo();
		}
		model.addAttribute("material", materialRespVo);
		model.addAttribute("materialCategorys", materialCategoryService.selectAllMaterialCategorys().getData());
		return "/base/add_update_material";
	}

	/**
	 * @param ids
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 删除素材
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteMaterial", method = RequestMethod.POST)
	public ApiResult deleteMaterial(String ids) {
		String[] idList = ids.split(",");
		// 批量删除
		LOGGER.info("ids", ids);
		try {
			return materialService.deleteMaterials(idList);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e:{}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}

	/**
	 * @param material
	 * @param imgUrlFile
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 保存
	 */
	@ResponseBody
	@RequestMapping(value = "/saveMaterial", method = RequestMethod.POST)
	public ApiResult saveRole(Material material, MultipartFile imgUrlFile) {
		LOGGER.info("material", material);
		try {
			String filePath = fileUtil.uploadFile(imgUrlFile);
			material.setImgUrl(filePath);
			return materialService.saveMaterial(material);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}
}
